#include "stm32.h"
#include "ui_stm32.h"

Stm32::Stm32(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Stm32)
{
    ui->setupUi(this);
    createSlideSwitch(ui->verticalLayout, 303, ui->labelWind);
    createSlideSwitch(ui->verticalLayout_2, 304, ui->labelCool);
    createSlideSwitch(ui->verticalLayout_3, 305, ui->labelHot);

    pngMap.insert(303,":/stm32/image/jiashi_off.png");
    pngMap.insert(304,":/stm32/image/wind_off.png");
    pngMap.insert(305,":/stm32/image/hot_off.png");

    pngOnMap.insert(303,":/stm32/image/jsashi_on.png");
    pngOnMap.insert(304,":/stm32/image/wind_on.png");
    pngOnMap.insert(305,":/stm32/image/hot_on.png");

}

Stm32::~Stm32()
{
    delete ui;
}

void Stm32::switchChangeSlot(bool checked)
{
    SlideSwitch *s = dynamic_cast<SlideSwitch*>(sender());
    bool is = s->getIsChecked();
    if(s->getIsChecked())
    {
        s->setBgColor("#409EFF");

    }
    else
    {
         s->setBgColor(QColor(220,223,230));
    }
    int key = s->property("key").toInt();

    if (labelMap.contains(key)) {
        QLabel *label = labelMap[key];
        if (label) {
            if (checked) {
                label->setPixmap(QPixmap(pngOnMap[key]));
                qDebug() << pngOnMap[key];
            } else {
                label->setPixmap(QPixmap(pngMap[key]));
                qDebug() << pngMap[key];
            }

        }
    }
    QJsonObject jsonObject;
    jsonObject["type"] = 2;
    QJsonObject dataObject;
    dataObject["key"] = s->property("key").toInt();
    dataObject["val"] = checked ? "1" : "0";
    jsonObject["data"] = dataObject;
    QJsonDocument jsonDoc(jsonObject);
    QByteArray jsonByteArray = jsonDoc.toJson(QJsonDocument::Indented);
    QMQTT::Message message;
    message.setTopic("/app/data/down");
    message.setPayload(jsonByteArray);
    GlobalVal::mqtt->publish(message);
}

void Stm32::createSlideSwitch(QBoxLayout *layout, int keyValue, QLabel *label)
{
    SlideSwitch *slideSwitch = new SlideSwitch(this);
    slideMap.insert(keyValue, slideSwitch);
    labelMap.insert(keyValue, label);
    slideSwitch->setProperty("key", keyValue);
    connect(slideSwitch, &SlideSwitch::checkedChanged, this, &Stm32::switchChangeSlot);
    slideSwitch->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    slideSwitch->setMinimumSize(QSize(64, 32));
    slideSwitch->setMaximumSize(QSize(64, 32));
    layout->addWidget(slideSwitch);
    layout->setStretch(0, 1);
    layout->setStretch(1, 4);
    layout->setStretch(2, 1);
}

void Stm32::switchCtlToUi(QJsonObject jsonData)
{
    if (jsonData.contains("data") && jsonData["data"].isArray()) {
        QJsonArray dataArray = jsonData["data"].toArray();
        for (const QJsonValue &value : dataArray) {
            if (value.isObject()) {
                QJsonObject obj = value.toObject();
                // 提取key、val和type字段并打印（或进行UI展示）
                int key = obj["key"].toInt();

                int type = obj["type"].toInt();
                if(slideMap.contains(key))
                {
                    QString text("true");
                    bool val = obj["val"] == text ? true : false;
                    if(val)
                    {
                        slideMap[key]->setBgColor("#409EFF");

                    }
                    else
                    {
                         slideMap[key]->setBgColor(QColor(220,223,230));
                    }
                    slideMap[key]->setChecked(val);
                    if (labelMap.contains(key)) {
                        QLabel *label = labelMap[key];
                        if (label) {
                            if (val) {
                                label->setPixmap(QPixmap(pngOnMap[key]));
                            } else {
                                label->setPixmap(QPixmap(pngMap[key]));
                            }
                        }
                    }
                }

            } else {
                // 如果值不是QJsonObject，则打印错误或进行其他处理
                qDebug() << "Unexpected value type in data array";
            }
        }
    } else {
        // 如果JSON对象中没有名为"data"的数组，则打印错误或进行其他处理
        qDebug() << "No 'data' array found in the JSON object";
    }
}
